package boot

import (
	"context"
	"log"

	"gin.andbridge/config"
	"gin.andbridge/utils/read"
	"github.com/go-redis/redis/v8"
)

type RedisDB struct {
	*config.Redis
}

func RedisInit() *RedisDB {
	reader := read.NewReader()
	redisConf := &config.Redis{}
	err := reader.ReadJustPiece("Redis", redisConf)
	if err != nil {
		log.Fatalf("读取redis信息失败%v", err)
	}
	return &RedisDB{redisConf}
}

func (redisDB *RedisDB) redisEngine() *redis.Client {
	db := redis.NewClient(&redis.Options{
		Addr:     redisDB.HostPort,
		Password: redisDB.Password,
		DB:       redisDB.WhichDb,
		PoolSize: redisDB.PoolSize,
	})
	context := context.Background()
	_, err := db.Ping(context).Result()
	if err != nil {
		log.Fatalf("redis连接失败%v", err)
	}
	log.Print("redis连接成功!")
	return db
}

func (redis *RedisDB) redisPrefix() string {
	return redis.Prefix
}
